﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityFramework;
using System.Collections.Generic;
using Channelvn.Cached.Common;

namespace ProjectQuaDep.Control
{
    public partial class ZoneProductMenu : System.Web.UI.UserControl
    {
        protected int ZoneStt = 0;
        protected int Stt = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtAllZone = BO.ProductUserBO.ProductUBO.GetListZoneByParentIDBO("0");
            rptList.DataSource = dtAllZone;//ZoneProductBO.GetListZoneProduct(0);
            rptList.DataBind();
            int ZoneID = Lib.QueryString.ZoneID;
            if (ZoneID == 0)
            {
                long productId = Lib.QueryString.ProductID;
                if (productId > 0)
                {
                    DataTable dtNew = BO.ProductUserBO.ProductUBO.GetInfomationProductBO(productId.ToString());
                    if (dtNew.Rows.Count > 0)
                    {
                        ZoneID =int.Parse(dtNew.Rows[0][BO.Const.tbProducts.ZoneId].ToString());
                    }
                }
            }
            //check zone chi tiet
            DataTable dtZone = BO.ProductUserBO.ProductUBO.GetInfomationZoneBO(ZoneID.ToString());
            
            if (dtZone.Rows.Count > 0)
            {
                if (dtZone.Rows[0][BO.Const.tbZoneProduct.ParentId].ToString() != "0")
                {
                    //zone con
                    ZoneStt = int.Parse(dtZone.Rows[0][BO.Const.tbZoneProduct.ParentId].ToString());
                }
                else
                {
                    //zone cha
                    ZoneStt = ZoneID;
                }
            }
            //lap de biet stt
            if (dtAllZone.Rows.Count > 0 && dtZone.Rows.Count > 0)
            {
                for (int i = 0; i < dtAllZone.Rows.Count; i++)
                {
                    if (ZoneStt.ToString() == dtAllZone.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString())
                    {
                        Stt = i + 1;
                        break;
                    }
                }
            }
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string id = Convert.ToString(drv.Row["Zoneid"]);

                //var id = ((ZoneProduct)e.Item.DataItem).ZoneId;

                if (!string.IsNullOrEmpty(drv.Row["Zoneid"].ToString()))
                {
                    //string companiesIds = Convert.ToString(drv.Row["CompanyID"]).TrimEnd(',');
                    List<Company> lst = CompaniesBO.GetCompaniesByZoneIds(int.Parse(id));
                    if (lst.Count > 0)
                    {
                        var rptCompanies = (Repeater)e.Item.FindControl("rptCompanies");
                        if (rptCompanies != null)
                        {
                            rptCompanies.DataSource = null;
                            rptCompanies.DataSource = lst;
                            rptCompanies.DataBind();
                            rptCompanies.Visible = true;
                        }
                    }
                }
            }
        }

        protected void rptCompanies_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var CompanyId = ((Company)e.Item.DataItem).CompanyID;
                var ZoneIDCha = ((Company)e.Item.DataItem).ZoneId;
                //List<ZoneProduct> lst = ZoneProductBO.GetListZoneProductByCompanyId(int.Parse(id.ToString()));
                //DataRowView drv = (DataRowView)e.Item.DataItem;
                //string CompanyId = Convert.ToString(drv.Row["CompanyID"]);
                DataTable lst = BO.ProductUserBO.ProductUBO.GetZoneByCompanyID(CompanyId.ToString());
                if (lst.Rows.Count > 0)
                {
                    DataTable FilterZoneID = new DataTable();
                    FilterZoneID.Columns.Add("ZoneIdOther");
                    FilterZoneID.Columns.Add("CompanyID");
                    FilterZoneID.Columns.Add("title");
                    FilterZoneID.Columns.Add("ZoneID");
                    for (int i = 0; i < lst.Rows.Count; i++)
                    {
                        string ZoneIDDt = lst.Rows[i]["ZoneID"].ToString().ToLower().Trim();
                        string ZoneIDChaF = ZoneIDCha.ToString().ToLower().Trim();
                        if (ZoneIDDt.Equals(ZoneIDChaF))
                        {
                            DataRow dr = FilterZoneID.NewRow();
                            dr["ZoneIdOther"] = lst.Rows[i]["ZoneIdOther"].ToString();
                            dr["CompanyID"] = lst.Rows[i]["CompanyID"].ToString();
                            dr["title"] = lst.Rows[i]["title"].ToString();
                            dr["ZoneID"] = lst.Rows[i]["ZoneID"].ToString();
                            FilterZoneID.Rows.Add(dr);
                        }

                    }

                    var rptListChild = (Repeater)e.Item.FindControl("rptListChild");
                    if (rptListChild != null)
                    {
                        rptListChild.DataSource = null;
                        rptListChild.DataSource = FilterZoneID;
                        rptListChild.DataBind();
                        rptListChild.Visible = true;
                    }
                }
            }
        }

        protected void rptListChild_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            //{
            //    var title = ((ZoneProduct)e.Item.DataItem).title;
            //    var
            //    Literal ltrLink = (Literal)e.Item.FindControl("ltrLink");
            //    ltrLink.Text = string.Format("<a href="/<%# Eval("url") %>.htm" title="<%# HttpUtility.HtmlEncode(Eval("title")) %>">") 
            //}
        }
    }
}